package mmi.gui.widget;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import javax.swing.JColorChooser;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;

import mmi.event.ColourChangedEvent;
import mmi.gui.MainFrame;

/**
 *
 * @author louis
 *
 */
public class ColourChooser extends JPanel implements MouseInputListener {
	enum ColourChooserStyle { PrepickedColours, LineFillPicker, Both }
	private static final long serialVersionUID = -5821047705202286654L;

        enum state { CONTENT, BORDER };
        state s = null;

	private ColourChooserStyle ccs = ColourChooserStyle.Both;

	MainFrame parent = null;

	Rectangle rs[] = null;

	Rectangle customColourRs = null;

	Color colours[] = {
            new Color(255, 255, 255), new Color(239, 173, 169), new Color(249, 195, 162), new Color(216, 197, 182), new Color(247, 247, 173), new Color(188, 223, 177), new Color(186, 231, 231), new Color(171, 209, 229), new Color(203, 185, 218), new Color(219, 164, 195),
            new Color(0, 0, 0), new Color(215, 50, 41),   new Color(241, 106, 21),  new Color(157, 110, 72),  new Color(237, 237, 49),  new Color(89, 176, 60),   new Color(84, 196, 196),  new Color(45, 141, 190),  new Color(124, 80, 164),  new Color(167, 27, 106),
            //new Color(141, 141, 141),       new Color(90, 21, 17),    new Color(101, 44, 8),    new Color(66, 46, 30),    new Color(99, 99, 20),    new Color(37, 74, 25),    new Color(35, 82, 82),    new Color(18, 59, 80),    new Color(52, 33, 69),    new Color(70, 11, 44)
        };

	public ColourChooser(MainFrame parent)
	{
		this.parent = parent;
		initialise();
	}
	public void colourChangedEventOccured(ColourChangedEvent evt)
	{
		repaint();
	}
	private void fillRect(Graphics g, Rectangle r)
	{
		g.fillRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight());
	}


	private void initialise()
	{
		setSize(800, 500);
                s = s.CONTENT;
		addMouseListener(this);
		//		panel = new JPanel();



	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		for(int i = 0; i < rs.length; i++)
		{
			if(rs[i].contains(e.getPoint()))
			{
                            if ( s == s.CONTENT ) {
				if(e.getButton() == MouseEvent.BUTTON3)
				{
					parent.getDrawingArea().setLineColour(colours[i]);
                                        parent.getDrawingBar().line.setBackground(parent.getDrawingArea().getLineColour());
					return;
				} else if(e.getButton() == MouseEvent.BUTTON1)
				{
					parent.getDrawingArea().setFillColour(colours[i]);
					return;
				}
                            }
                            if ( s == s.BORDER ) {
				if(e.getButton() == MouseEvent.BUTTON1)
				{
					parent.getDrawingArea().setLineColour(colours[i]);
                                        parent.getDrawingBar().line.setBackground(parent.getDrawingArea().getLineColour());
					return;
				} else if(e.getButton() == MouseEvent.BUTTON3)
				{
					parent.getDrawingArea().setFillColour(colours[i]);
					return;
				}
                            }
			}
		}


		if(customColourRs.contains(e.getPoint()) && s == s.CONTENT)
		{
			if( e.getButton() == MouseEvent.BUTTON1 )
			{
				parent.getDrawingArea().setFillColour(JColorChooser.showDialog(ColourChooser.this, "Choose fill colour", parent.getDrawingArea().getFillColour()));
				return;
			}
                        if( e.getButton() == MouseEvent.BUTTON3 )
			{
				parent.getDrawingArea().setLineColour(JColorChooser.showDialog(ColourChooser.this, "Choose line colour", parent.getDrawingArea().getLineColour()));
				parent.getDrawingBar().line.setBackground(parent.getDrawingArea().getLineColour());
                                return;
			}
		} else if(customColourRs.contains(e.getPoint()) && s == s.BORDER)
		{
			if(e.getButton() == MouseEvent.BUTTON1)
			{
				parent.getDrawingArea().setLineColour(JColorChooser.showDialog(ColourChooser.this, "Choose line colour", parent.getDrawingArea().getLineColour()));
				parent.getDrawingBar().line.setBackground(parent.getDrawingArea().getLineColour());
                                return;
			}
                        if( e.getButton() == MouseEvent.BUTTON3 )
			{
				parent.getDrawingArea().setFillColour(JColorChooser.showDialog(ColourChooser.this, "Choose fill colour", parent.getDrawingArea().getFillColour()));
                                return;
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	protected void paintComponent(Graphics g)
	{
		int rectWidth = (int) Math.min(getWidth() / 6 * 0.8, getHeight()/2 * 0.8);
		int seperation = (int) Math.min(getWidth() / 6 * 0.2, getHeight()/2 * 0.2);
		int xOffset = 0;

		//customColourRs = new Rectangle();
		//		LineFillPicker
		if(ccs.equals(ColourChooserStyle.Both) || ccs.equals(ColourChooserStyle.LineFillPicker))
		{
                    g.setColor(parent.getDrawingArea().getLineColour());
                    customColourRs = new Rectangle((int) (1*seperation), (int) (1*seperation), (int)(2*rectWidth), (int)(2*rectWidth));
                    fillRect(g, customColourRs);

                    g.setColor(parent.getDrawingArea().getFillColour());
                    customColourRs = new Rectangle((int) (2*seperation), (int) (2*seperation), (int)(1.5*rectWidth), (int)(1.5*rectWidth));
                    fillRect(g, customColourRs);
		}

		//		PrepickedColourPicker
		if(ccs.equals(ColourChooserStyle.Both) || ccs.equals(ColourChooserStyle.LineFillPicker))
		{
			xOffset = 2*rectWidth + 2*seperation;
		}
		if(ccs.equals(ColourChooserStyle.Both) || ccs.equals(ColourChooserStyle.PrepickedColours))
		{

			boolean fill = false;
			if(rs == null)
			{
				rs = new Rectangle[20];
				fill = true;
			}

			for(int i = 0; i < 2; i++)
			{
				for(int j = 0; j < 10; j++)
				{
					g.setColor(colours[(i*10+j)]);
					if(fill)
					{
						rs[i*10+j] = new Rectangle((int) (xOffset+(j+0.5)*seperation+j*rectWidth), (int) (i*rectWidth+(i+0.5)*seperation), rectWidth, rectWidth);
					} else
					{
						fillRect(g, rs[i*10+j]);
					}
				}
			}
		}
	}

}
